﻿using HslCommunication;
using HslCommunication.Profinet.Omron;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace lycServo.client
{
    public class OmronClient
    {
        private OmronConnectedCipNet _master;
        private readonly string _unitNo;
        private readonly string _host;
        private readonly int _port;
        private Action<string, string, string> _showMessage;

        private bool _isConnected = false;

        public OmronClient(string unitNo, string host, int port, Action<string, string, string> showMessage)
        {
            this._unitNo = unitNo;
            this._host = host;
            this._port = port;
            _showMessage = showMessage;
        }

        public void Start()
        {
            //创建TCP客户端
            try
            {
                Connect();
            }
            catch (Exception)
            {

            }
            finally
            {
                DisconnectCallback();
            }
        }

        private void Connect()
        {
            try
            {
                _master = new OmronConnectedCipNet(_host);
                OperateResult connResult = _master.ConnectServer();
                if (connResult.IsSuccess)
                {
                    _isConnected = true;
                    ShowMessage("0", $"与设备[{_unitNo}]连接成功!_isConnected=True");
                }
                else
                {
                    _isConnected = false;
                }
            }
            catch (Exception ex)
            {
                _isConnected = false;
                throw new Exception($"与设备[{_unitNo}]连接失败:{ex.Message}");
            }
        }

        public string ReadData(string label, int length)
        {
            OperateResult<short[]> operateResult = _master.ReadInt16(label, (ushort)length);
            if (operateResult.IsSuccess)
            {
                short[] vs = operateResult.Content.ToArray();
                return vs[0].ToString();
            }
            _isConnected = false;
            throw new Exception($"读取信号点{label}失败!");
        }

        public void WriteData(string label, short data)
        {
            OperateResult operateResult = _master.Write(label, data);
            if (!operateResult.IsSuccess)
            {
                _isConnected = false;

                throw new Exception("写入数据失败");
            }
        }

        public bool HasStarted()
        {
            return _isConnected;
        }

        #region ===连接守护方法===
        public void DisconnectCallback()
        {
            new Thread(() =>
            {
                while (true)
                {
                    IPStatus iPStatus = _master.IpAddressPing();
                    if (iPStatus == IPStatus.Success && _isConnected)
                    {
                        _isConnected = true;
                        Thread.Sleep(30 * 1000);
                        continue;
                    }
                    Connect();
                    Thread.Sleep(5 * 1000);
                }
            }).Start();
        }
        #endregion

        private void ShowMessage(string code, string message)
        {
            _showMessage?.Invoke(_unitNo, code, message);
        }
    }
}
